package actor

import (
	"fmt"
	"sync"
)

var Directors Director
var initCallBack = make([]func(), 0)

type Director struct {
	NodeName string
	ActorMap sync.Map
	IncrPid  int64 // 自增pid
}

func init() {
	Directors = Director{
		NodeName: "local:game",
		ActorMap: sync.Map{},
		IncrPid:  0,
	}
}
func NewPid() int64 {
	Directors.IncrPid++
	return Directors.IncrPid
}

func RegisterActor(pid int64, iActor IActor) {
	Directors.ActorMap.Store(pid, iActor)
}

func RegisterInitActor(f func()) {
	initCallBack = append(initCallBack, f)
}

func Init() {
	for _, init := range initCallBack {
		init()
	}
}

func SaveAllActor() {
	Directors.ActorMap.Range(func(key, value interface{}) bool {
		actor := value.(IActor)
		actor.Loader()
		fmt.Println("Stop save actor", actor)
		return true
	})
}
